﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class UserControl_Backup : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PathText.Text = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
            String pathname, drivername, currentdriver;
            for (int i = 0; i < DriveInfo.GetDrives().Length; i++)
            {
                drivername = DriveInfo.GetDrives().GetValue(i).ToString();
                DriveInfo driveinfo = new DriveInfo(drivername);
                if (driveinfo.DriveType == DriveType.Fixed)
                {                    
                    DriverList.Items.Add(drivername);
                }
            }
            currentdriver = Directory.GetDirectoryRoot(PathText.Text);
            DriverList.Text = currentdriver;
            DirectoryInfo parentinfo = new DirectoryInfo(Directory.GetParent(PathText.Text).ToString());
            if (parentinfo.Attributes == FileAttributes.Directory)
            {
                ListItem dirup = new ListItem("..", Directory.GetParent(PathText.Text).ToString());
                DirectoryList.Items.Add(dirup);
            }
            for (int i = 0; i < Directory.GetDirectories(PathText.Text).Length; i++)
            {
                pathname = Directory.GetDirectories(PathText.Text).GetValue(i).ToString();                
                DirectoryInfo dirinfo = new DirectoryInfo(pathname);
                if (dirinfo.Attributes == FileAttributes.Directory)
                {
                    ListItem diritem = new ListItem(Path.GetFileName(pathname), Path.GetFullPath(pathname));
                    DirectoryList.Items.Add(diritem);
                }
            }
        }
    }
    protected void DriverList_SelectedIndexChanged(object sender, EventArgs e)
    {
        String pathname;
        PathText.Text = DriverList.Text;
        DirectoryList.Items.Clear();
        for (int i = 0; i < Directory.GetDirectories(PathText.Text).Length; i++)
        {
            pathname = Directory.GetDirectories(PathText.Text).GetValue(i).ToString();
            DirectoryInfo dirinfo = new DirectoryInfo(pathname);
            if (dirinfo.Attributes == FileAttributes.Directory)
            {
                ListItem diritem = new ListItem(Path.GetFileName(pathname), Path.GetFullPath(pathname));
                DirectoryList.Items.Add(diritem);
            }
        }
    }
    protected void DirectoryList_SelectedIndexChanged(object sender, EventArgs e)
    {        
        if (DirectoryList.SelectedItem.Text == ".." )
        {
            String parentdir = Directory.GetParent(DirectoryList.SelectedItem.Value).ToString();
            DirectoryInfo dirinfo = new DirectoryInfo(parentdir);
            if (dirinfo.Attributes == FileAttributes.Directory)
            {
                PathText.Text = parentdir + Path.DirectorySeparatorChar;
                DirectoryList.Items.Clear(); 
                ListItem dirup = new ListItem("..", Directory.GetParent(PathText.Text).ToString());
                DirectoryList.Items.Add(dirup);
            }
            else
            {
                PathText.Text = parentdir;
                DirectoryList.Items.Clear();
            }
        }
        else
        {
            PathText.Text = DirectoryList.SelectedValue + Path.DirectorySeparatorChar;
            DirectoryList.Items.Clear();
            ListItem dirup = new ListItem("..", Directory.GetParent(PathText.Text).ToString());
            DirectoryList.Items.Add(dirup);
        }        
        for (int i = 0; i < Directory.GetDirectories(PathText.Text).Length; i++)
        {
            String pathname = Directory.GetDirectories(PathText.Text).GetValue(i).ToString();
            DirectoryInfo dirinfo = new DirectoryInfo(pathname);
            if (dirinfo.Attributes == FileAttributes.Directory)
            {
                ListItem diritem = new ListItem(Path.GetFileName(pathname),Path.GetFullPath(pathname));
                DirectoryList.Items.Add(diritem);
            }
        }
    }
    protected void ButtonComfirm_Click(object sender, EventArgs e)
    {    
        Backing.Visible = true;
        bool Msg;
        Msg = Utilities.DatabaseBackup(ConfigurationManager.ConnectionStrings["BookShop"].ConnectionString, PathText.Text, FileName.Text);
        if (Msg)
        {
            Backing.Visible = false;
            BackupSuccess.Visible = true;
        }
        else
        {
            Backing.Visible = false;
            BackupSuccess.Visible = false;
        }
    }
    protected void PathExists_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            args.IsValid = Directory.Exists(PathText.Text);
        }
        catch (Exception ex)
        {
            args.IsValid = false;
            throw ex;
        }
    }
}
